package org.example.controller;

import jakarta.servlet.http.HttpServletResponse;
import org.example.service.IRapidOcrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.UnsupportedEncodingException;
import java.util.List;

@RestController
@RequestMapping("/ocr")
public class OcrController {

    @Autowired
    private IRapidOcrService rapidOcrService;

    /**
     * pdfOcr识别
     */
    @PostMapping("/pdf")
    public String pdfOcr(@RequestParam("file") MultipartFile fileUpload) {
        List<String> images = rapidOcrService.pdfToImage(fileUpload);
        return rapidOcrService.doOcr(images);
    }

    /**
     * 单张图片识别
     */
    @PostMapping("/img")
    public String imgOcr(@RequestParam("file") MultipartFile fileUpload) {
       return rapidOcrService.doImageOcr(fileUpload);
    }

    /**
     * pdf转word
     */
    @PostMapping("/pdfToWord")
    public void pdfToWord(@RequestParam("file") MultipartFile file, HttpServletResponse response) throws UnsupportedEncodingException {
        // 调用pdf2Word 方法
        rapidOcrService.pdfToWord(file, response);
    }

    /**
     * word转pdf
     * @param file
     * @param response
     */
    @PostMapping("/wordToPdf")
    public void wordToPdf(@RequestParam("file") MultipartFile file, HttpServletResponse response) {
        // 调用 convertWordToPdf 方法
        rapidOcrService.convertWordToPdf(file, response);
    }
}
